﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SharpMap.Layers;
using SharpMap.Data.Providers;
using System.IO;

namespace SharpMapControlWindow
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ShapeFile shp = new ShapeFile(@"D:\GeoBeans\data\world\country.shp");
            VectorLayer layer = new VectorLayer("country");
            layer.DataSource = shp;
            this.axSharpMapControl1.AddLayer(layer);
            this.axSharpMapControl1.ZoomToExtents();
            this.axSharpMapControl1.RefreshMap();
        }

        private void btnAddRasterLayer_Click(object sender, EventArgs e)
        {
            string path = @"E:\Research\CSIRO\AHP-SA\MapPlatform\bin\Debug\Maps\landuse_100m_CopyRaster.tif";
            //string path = @"C:\Users\Renyc\Pictures\Koala.bmp";
            GdalRasterLayer layer = new GdalRasterLayer("world", path);
            this.axSharpMapControl1.AddLayer(layer);
            this.axSharpMapControl1.ZoomToExtents();
            this.axSharpMapControl1.RefreshMap();

            OSGeo.GDAL.Dataset dataset = layer.Dataset;
            int count = 0;
            count = dataset.RasterCount;
            ReadRasterDataSet(dataset);
        }

        private void btnZoomIn_Click(object sender, EventArgs e)
        {
            this.axSharpMapControl1.ZoomIn();
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            this.axSharpMapControl1.RefreshMap();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.axSharpMapControl1.ZoomOut();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.axSharpMapControl1.Pan();
        }

        //public CPLErr ReadRaster(int xOff, int yOff, int xSize, int ySize, byte[] buffer, int buf_xSize, int buf_ySize, int pixelSpace, int lineSpace);
        private void ReadRasterDataSet(OSGeo.GDAL.Dataset dataset)
        {
            OSGeo.GDAL.Band band = dataset.GetRasterBand(1);

            int xOff = 0;
            int yOff = 0;
            int xsize = dataset.RasterXSize;
            int ysize = dataset.RasterYSize;
            double[] buffer = new double[xsize*ysize];
            int buf_xSize = dataset.RasterXSize;
            int buf_ySize = dataset.RasterYSize;
            int pixelSpace = 0;
            int lineSpace = 0;
            OSGeo.GDAL.CPLErr err;

            err = band.ReadRaster(xOff, yOff, xsize, ysize, buffer, buf_xSize, buf_ySize, pixelSpace, lineSpace);
            if (err != OSGeo.GDAL.CPLErr.CE_None)
            {
                Console.WriteLine("error");
            }

            StreamWriter writer = File.CreateText(@"G:\temp\raster.txt");
            for (int i = 0; i < xsize; i++)
            {
                for (int j = 0; j < ysize; j++)
                {
                    int value = (int)(buffer[i * ysize + j]);
                    if(value==256)
                        writer.Write((int)0);
                    else
                        writer.Write(value);
                    writer.Write(",");
                }
                writer.Write("\n");
            }
            writer.Close();
            
        }
    }
}
